﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;

namespace FrankensteamClient
{
    class TabHandler : TabItem
    {
        
        // Constructor
        public TabHandler()
        {
            // Create an instance of the usercontrol
            TabHeader tabHeader = new TabHeader();

            // Assign the usercontrol to the tab header
            this.Header = tabHeader;

            // Attach to the TabHeader events (Mouse Enter/Leave, Button Click, and Label resize)
            tabHeader.btnCloseTab.MouseEnter += new MouseEventHandler(button_close_MouseEnter);
            tabHeader.btnCloseTab.MouseLeave += new MouseEventHandler(button_close_MouseLeave);
            tabHeader.btnCloseTab.Click += new RoutedEventHandler(button_close_Click);
            tabHeader.lblTabTitle.SizeChanged += new SizeChangedEventHandler(label_TabTitle_SizeChanged);            
        }


        /// <summary>
        /// Property - Set the Title of the Tab
        /// </summary>
        public string Title
        {
            set
            {
                ((TabHeader)this.Header).lblTabTitle.Content = value;
            }
            get
            {
                string temp = "";
                if (!this.Dispatcher.CheckAccess())
                {
                    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    temp = ((TabHeader)this.Header).lblTabTitle.Content.ToString();
                }));
                }
                else
                {
                    temp = ((TabHeader)this.Header).lblTabTitle.Content.ToString();
                }
                return temp;
            }
        }

        //
        // - - - Overrides  - - -
        //


        // Override OnSelected - Show the Close Button
        protected override void OnSelected(RoutedEventArgs e)
        {
            base.OnSelected(e);
            ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Visible;
        }

        // Override OnUnSelected - Hide the Close Button
        protected override void OnUnselected(RoutedEventArgs e)
        {
            base.OnUnselected(e);
            ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Hidden;
        }

        // Override OnMouseEnter - Show the Close Button
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Visible;
        }

        // Override OnMouseLeave - Hide the Close Button (If it is NOT selected)
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            if (!this.IsSelected)
            {
                ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Hidden;
            }
        }


        //
        // - - - Event Handlers  - - -
        //


        // Button MouseEnter - When the mouse is over the close button - change color to white
        void button_close_MouseEnter(object sender, MouseEventArgs e)
        {
            ((TabHeader)this.Header).btnCloseTab.Foreground = Brushes.White;
        }

        // Button MouseLeave - When mouse is no longer over close button - change color to gray 
        void button_close_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TabHeader)this.Header).btnCloseTab.Foreground = Brushes.Gray;
        }


        // Button Close Click - Remove the Tab - (or raise an event indicating a "CloseTab" event has occurred)
        void button_close_Click(object sender, RoutedEventArgs e)
        {
            if (this.Title != "ALL")
            ((TabControl)this.Parent).Items.Remove(this);
        }


        // Label SizeChanged - When the Size of the Label changes (due to setting the Title) set position of button properly
        void label_TabTitle_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ((TabHeader)this.Header).btnCloseTab.Margin = new Thickness(((TabHeader)this.Header).lblTabTitle.ActualWidth + 10, 3, 4, 0);
        }
    }
}
